// import axios from 'axios' //idnex.html首页引入之后。这里不需要了
// import Vue from 'vue' //idnex.html首页引入之后。这里不需要了
// import Qs from Qs'
import router from '../router/routerscfg'
import store from '@/vuex/store'
import {Message} from "element-ui"
import URLS from "./interface"
import tableConfig from "./tableConfig"

//系统提示常量
var Message_STATUS={
  "serviceClose":"服务器已关闭",
  "serviceUnKnowError":"请求未知错误",
  "serviceNotFound":"找不到该服务",
}

var msgError = function (msg){
  Message.error({
    message:msg,
    duration: 2500
  })
  throw new Error(msg);
}
var msgWaring = function (msg){
  Message.warning({
    message:msg,
    duration: 2500
  })
  throw new Error(msg);
}

var instance = axios.create({
  baseURL: process.env.BASE_API, // api的base_url
  timeout: 60000,
  withCredentials: true
})

instance.interceptors.request.use(config => {
  // console.log("请求开始前"+Qs.stringify(config));
  // // console.log("请求前的laoding0000000",store.state.loadingMap)
  return config
}, error => {
  // console.log("请求前错误"+error);
  return Promise.reject(error);
})

instance.interceptors.response.use(res => {
  // console.log(res);
  store.commit("filterLoaingMap",res.config.url);
  // console.log(res.config.url);
  // console.log(store.state.loadingMap);
  // console.log("返回结果前"+res.data);
  return checkStatus(res);
}, error => {
  // console.log("返回报错");
  console.log(error);
  console.log("返回报错");
  if(error && error.response){
    store.commit("filterLoaingMap",error.response.request.responseURL);
    if(error.response.status===404){
      msgError(Message_STATUS.serviceNotFound);
    }else{
      msgError(error.response.data.message);
    }
  }else{
    store.commit("clearLoadingMap");
    // msgError(Message_STATUS.serviceClose);
  }
})

//检查返回状态
function checkStatus(res) {
  //每交互一次就更新一下当前本地的过期token时间；不需要了
  // if(res.config.url.indexOf(URLS.login)==-1){
  //   store.commit("updateToken",res.data);
  // }
  console.log(res);
  if ((res.status == 200 || res.status == 304)) {
    if(res.data.code==0||!res.data.code){
      return res.data;
    }else if(res.data.code>=1000&&res.data.code<=1099){
      //需要需要清除登陆
      store.commit("clearToken");
      router.replace("/login");
      if(res.data.serviceTime-store.exptime>5000){
        store.exptime=res.data.serviceTime;
        msgWaring(res.data.message);
      }
    }else{
      if(res.data.data!=null){
          msgWaring(res.data.data);
      }else{
        msgWaring(res.data.message);
      }
    }
  }else{
    msgWaring(Message_STATUS.serviceUnKnowError);
  }
  throw new Error();
}

/**
 * IE 浏览器的get请求会主动根据url缓存结果。导致相同的url不更新请求数据。在末尾加上一个时间戳
 */

export default {
  RESOURCE_URL:process.env.RESOURCE_DOMAIN,
  BASE_URL:process.env.BASE_API,
  WS_DOMAIN:process.env.WS_DOMAIN,
  defaultimgbase64:``,
  getAllJson(reqArr) {
    return axios.all(reqArr);
  },
  getJson(url,params) {
    // console.log("get带参",params==undefined,params==null);
    if(params==undefined||params==null){
      return instance({
        method: 'get',
        url:url+"?t="+new Date().getTime(),
        headers: {
          //'X-Requested-With': 'XMLHttpRequest'
          'CSRFToken':store.state.CSRFToken
        }
      })
    }else{
      return instance({
        method: 'get',
        url:url+"?"+Qs.stringify(params,{allowDots:true})+"&t="+new Date().getTime(),
        // params:params,
        headers: {
          //'X-Requested-With': 'XMLHttpRequest'
          'CSRFToken':store.state.CSRFToken
        }
      })
    }
  },
  getFile(url,params) {
    // console.log("get带参",params==undefined,params==null);
    if(params==undefined||params==null){
      return instance({
        method: 'get',
        responseType:"blob",
        url:url+"?t="+new Date().getTime(),
        headers: {
          //'X-Requested-With': 'XMLHttpRequest'
          'CSRFToken':store.state.CSRFToken
        }
      })
    }else{
      return instance({
        method: 'get',
        responseType:"blob",
        url:url+"?"+Qs.stringify(params,{allowDots:true})+"&t="+new Date().getTime(),
        // params:params,
        headers: {
          //'X-Requested-With': 'XMLHttpRequest'
          'CSRFToken':store.state.CSRFToken
        }
      })
    }
  },
  postJson(url, data) {
    return instance({
      method: 'post',
      url:url,
      data: Qs.stringify(data),
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'CSRFToken':store.state.CSRFToken
      }
    })
  },
  postJsonObject(url, data) {
    return instance({
      method: 'post',
      url:url,
      data: Qs.stringify(data,{allowDots:true}),
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'CSRFToken':store.state.CSRFToken
      }
    })
  },
  postFile(url, data) {
    return instance({
      method: 'post',
      url:url,
      data:data,
      headers: {
        'X-Requested-With': 'XMLHttpRequest',
        'CSRFToken':store.state.CSRFToken
      }
    })
  },
  postJsonAndFile(url, data) {
    return instance({
      method: 'post',
      url:url,
      data:data,
      headers: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'CSRFToken':store.state.CSRFToken
      }
    })
  },
  URLS:URLS,
  tableConfig:tableConfig
}
